﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

using System.Configuration.Install;
using System.Collections;
using System.IO;

namespace Pilot_Service
{
    static class Program
    {
        private static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();

        static void InternalMain(string[] args)
        {
            if (args.Length == 1)
            {
                try
                {
                    using (TransactedInstaller ti = new TransactedInstaller())
                    {
                        using (ProjectInstaller pi = new ProjectInstaller())
                        {
                            ti.Installers.Add(pi);
                            string[] cmdline = { string.Format("/assemblypath={0}", System.Reflection.Assembly.GetExecutingAssembly().Location) };
                            pi.Context = new InstallContext(null, cmdline);
                            if (args[0].ToLower() == "/install")
                                pi.Install(new Hashtable());
                            else if (args[0].ToLower() == "/uninstall")
                                pi.Uninstall(null);
                            else
                                throw new Exception("Invalid command line");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                }
            }
            else
            {
                System.ServiceProcess.ServiceBase[] ServicesToRun;
                ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Pilot_Service() };
                System.ServiceProcess.ServiceBase.Run(ServicesToRun);
            }
        }

        static void InternalMain2(string[] args)
        {
            ArrayList myOptions = new ArrayList();
            String myOption;
            bool toUnInstall = false;
            bool toPrintHelp = false;
            TransactedInstaller myTransactedInstaller = new TransactedInstaller();
            AssemblyInstaller myAssemblyInstaller;
            InstallContext myInstallContext;

            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    // Process the arguments.
                    if (args[i].StartsWith("/") || args[i].StartsWith("-"))
                    {
                        myOption = args[i].Substring(1);
                        // Determine whether the option is to 'uninstall' an assembly.
                        if (String.Compare(myOption, "u", true) == 0 ||
                           String.Compare(myOption, "uninstall", true) == 0)
                        {
                            toUnInstall = true;
                            continue;
                        }
                        // Determine whether the option is for printing help information.
                        if (String.Compare(myOption, "?", true) == 0 ||
                           String.Compare(myOption, "help", true) == 0)
                        {
                            toPrintHelp = true;
                            continue;
                        }
                        // Add the option encountered to the list of all options
                        // encountered for the current assembly.
                        myOptions.Add(myOption);
                    }
                    else
                    {
                        // Determine whether the assembly file exists.
                        if (!File.Exists(args[i]))
                        {
                            // If assembly file doesn't exist then print error.
                            Console.WriteLine("\nError : {0} - Assembly file doesn't exist.",
                               args[i]);
                            return;
                        }

                        // Create a instance of 'AssemblyInstaller' that installs the given assembly.
                        myAssemblyInstaller =
                           new AssemblyInstaller(args[i],
                           (string[])myOptions.ToArray(typeof(string)));
                        // Add the instance of 'AssemblyInstaller' to the 'TransactedInstaller'.  
                        myTransactedInstaller.Installers.Add(myAssemblyInstaller);
                    }
                }
                // If user requested help or didn't provide any assemblies to install
                // then print help message.
                if (toPrintHelp || myTransactedInstaller.Installers.Count == 0)
                {
                    //PrintHelpMessage();
                    log.Info("PrintHelpMessage()");
                    return;
                }

                // Create a instance of 'InstallContext' with the options specified.
                myInstallContext =
                   new InstallContext("Install.log",
                   (string[])myOptions.ToArray(typeof(string)));
                myTransactedInstaller.Context = myInstallContext;

                // Install or Uninstall an assembly depending on the option provided.
                if (!toUnInstall)
                    myTransactedInstaller.Install(new Hashtable());
                else
                    myTransactedInstaller.Uninstall(null);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nException raised : {0}", e.Message);
            }  
        }

        /// <summary>
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// </summary>
        static void Main(string[] args)
        {
            log.Info("Begin....{0}", args.Length);
            InternalMain(args);
            log.Info("End....");
        }
    }
}
